package com.briup.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.function.Function;

/**
 * @Classname JDBCUtil
 * @Description TODO
 * @Date 2023/11/21 15:50
 * @Created by 崔宗祺
 * @Version v1.0.0
 */
public class JDBCUtil {
    private static Properties p = new Properties();
    static DataSource dataSource = null;

    static {
        //获取链接  通过来连接池
        //读取文件
        InputStream stream = JDBCUtil.class.getClassLoader().getResourceAsStream("druid.properties");
        try {
            //将文件读取到集合
            p.load(stream);
            //创建连接池
            dataSource = DruidDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取数据库链接  通过数据连接池
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    public static PreparedStatement getStatement(String sql) {
        try {
            return getConnection().prepareStatement(sql);
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    public static Statement getStatement() {
        try {
            return getConnection().createStatement();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    public static void executeDDL(String sql) {
        PreparedStatement ps = getStatement(sql);
        try {
            ps.execute();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
    }

    public static int executeDML(String sql) {

        PreparedStatement ps = getStatement(sql);
        try {
            return ps.executeUpdate();
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return 0;
    }

    public static <T> List<T> executeDQL(String sql, Function<ResultSet, T> function) {
        ArrayList<T> list = new ArrayList<>();
        PreparedStatement ps = getStatement(sql);
        try {
            ResultSet resultSet = ps.executeQuery();
            while (resultSet.next()) {
                final T t = function.apply(resultSet);
                list.add(t);
            }
            return list;
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
        return null;
    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet) {

        try {
            if (connection != null) {
                connection.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (resultSet != null) {
                resultSet.close();
            }
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        }
    }

    public static void close(Connection connection, Statement statement) {
        close(connection, statement, null);
    }
}
